package batch.RusSieDaemon;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.apache.log4j.Logger;
import batch.common.report.ReportError;

public class SieDaemon {
    boolean isReported = false;
    
	public void start() {
		while (true) {
			try {
						
			    try {
					SieXMLParsingForRus  sieXMLParsingForRus = new SieXMLParsingForRus();	
					sieXMLParsingForRus.xmlParsing_start();
				}
				catch (Exception e) {
					// file log
					e.printStackTrace();
					// report by email
					reportError("Error of RusSieDaemon in position 1", e);
				}
				finally {
					
				}
				Logger.getLogger("SieDaemon.run").info("Sleep 5 seconds!!!");
				Thread.sleep(5000);
			}
			catch (Exception e) {
				// file log
			    e.printStackTrace();
			    // report by email
			    reportError("Error of RusSieDaemon in position 2", e);
			}
		}
	}
	
	private void reportError(String sTitle, Exception e) {
	    if (!isReported) {
	        ByteArrayOutputStream baos = new ByteArrayOutputStream();
	        e.printStackTrace(new PrintStream(baos));
	        ReportError.byEmail(sTitle, baos.toString()); //later email
	        isReported = true;
	    }
	}

	public static void main(String[] args) {
		SieDaemon daemon = new SieDaemon();
		daemon.start();
	}
}
